package com.example.demo.controller;

import java.io.IOException;
import java.util.Objects;

import javax.annotation.Resource;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.BaseResult;
import com.example.demo.domain.RepairProcess;
import com.example.demo.service.RepairProcessService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *  @author w
 */
@RestController
@CrossOrigin
@Api(tags = "修程表相关接口", description = "提供修程表相关的 Rest API")
@RequestMapping({"/repairProcess"})
public class RepairProcessController {

    @Resource
    RepairProcessService repairProcessService;

    @ApiOperation("查找全部")
    @GetMapping("/findAll")
    public BaseResult getAll(){ 
        return BaseResult.buildSuccessData(repairProcessService.getAll());
    }

    @ApiOperation("根据车号查找")
    @GetMapping("/findByTrainId")
    public BaseResult getTrainId(@RequestParam(value = "trainId",required=false,defaultValue = "") String trainId){
        return BaseResult.buildSuccessData(repairProcessService.getTrainId(trainId));
    }

    @ApiOperation("根据年份查找")
    @GetMapping("/findByYear")
    public BaseResult getYear(@RequestParam(value = "year",required=false,defaultValue = "") String year){
        return BaseResult.buildSuccessData(repairProcessService.getYear(year));
    }

    @ApiOperation("新增数据")
    @PostMapping("/add")
    public BaseResult store(@RequestBody RepairProcess repairProcess){
//        repairProcess.setState(0);
        RepairProcess save = repairProcessService.add(repairProcess);
        return BaseResult.buildSuccessData(save);
    }

    @ApiOperation("根据ID修改数据")
    @PutMapping("/update/{id}")
    public BaseResult update(@PathVariable Integer id, @RequestBody RepairProcess repairProcess) {
        RepairProcess save = null;
        try {
            save = repairProcessService.update(id, repairProcess);
        } catch (Exception e) {
            return BaseResult.failure("id不存在");
        }
        return BaseResult.buildSuccessData(save);
    }

    @ApiOperation("根据车号删除数据")
    @DeleteMapping("/delete")
    public BaseResult destory(@RequestParam(value = "trainId",required = false,defaultValue = "") String trainId){
        String res = repairProcessService.destory(trainId);
        if(!Objects.equals(res, "true")){
            return BaseResult.failure(res);
        }
        return BaseResult.buildSuccessData(trainId.toString());
    }
    
    @ApiOperation("poi测试")
    @GetMapping("/poiTest")
    public String poiTest() throws IOException{
    	XSSFWorkbook workbook = new XSSFWorkbook("E:\\test.xlsx");
    	XSSFSheet sheet = workbook.getSheetAt(0);
    	int lastRowNum = sheet.getLastRowNum();
    	for(int i=0;i<=lastRowNum;i++){
    		XSSFRow row = sheet.getRow(i);
    		short lastCellNum = row.getLastCellNum();
    		for(short j=0;j<lastCellNum;j++){
    			String value = row.getCell(j).getStringCellValue();
    			System.out.println(value);
    		}
    	}
    	workbook.close();
        return "poi";
    }
    
}